﻿using FX_API.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static FX_API.Delegates;

namespace FX_API.Request
{
    public abstract class FXBaseUpdateService<FX_MODEL> where FX_MODEL : IObjectData, new()
    {
        public HandlerFunc InfoFunc { protected get; set; }
        public HandlerFunc ErrFunc { protected get; set; }
        public HandlerFunc WarningFunc { protected get; set; }

        protected readonly FXCommonUpdateRequest request;

        protected FXBaseUpdateService()
        {
            request = new FXCommonUpdateRequest
            {
                exceptionHandler = (Exception ex) =>
                {
                    string reason = $"执行{GetType().Name}时发生程序错误;";
                    string content = $"错误信息:{ex.Message},错误堆栈:{ex.StackTrace}";
                    ErrFunc?.Invoke(reason, content);
                },
                httpErrHandler = (string responseMsg, FXUpdateRequestModel request) =>
                {
                    string reason = $"执行{GetType().Name}时发生请求错误;";
                    string content = $"返回信息:{responseMsg},请求体:{request}";
                    ErrFunc?.Invoke(reason, content);
                }
            };
        }

        public virtual FXUpdateResponseModel Update(FX_MODEL model, object details=null, bool triggerWorkFlow = false)
        {
            return request.Update(model, details, triggerWorkFlow);
        }
    }
}
